limsProductMain.editPage = {

    getGridParam : function() {
        var _this = this;
        return {
            type : 'tab',
            shrink : false,
            showIndex : 0,
            fill : true,
            onActived: function (data) {
                _this.param.tabIndex = data.index;
                let grid;
                if (_this.param.tabIndex === 0){
                    grid = Gikam.getComp('lims-product-main-edit-list-grid');
                }else if (_this.param.tabIndex === 1){
                    grid = Gikam.getComp('lims-product-main-disable-list-grid');
                }
                if (Gikam.isEmpty(grid)){
                    return;
                }
                let activeRow = grid.getActivedRow();
                if (!Gikam.isEmpty(activeRow)) {
                    _this.loadProductList(activeRow);
                } else {
                    _this.loadProductList();
                }
            },
            panels : [{
                title : 'GIKAM.ACTIVE.STATUS.YES',
                items : [{
                    type : 'grid',
                    id : 'lims-product-main-edit-list-grid',
                    url : limsProductMain.baseUrl + '/queries/raw',
                    requestData : {
                        status_EQ : '1'
                    },
                    service : 'limsProductMainServiceImpl',
                    dbTable : 'T_LIMS_PRODUCT_MAIN',
                    columns : limsProductMain.getEditListColumns(),
                    page : true,
                    deleteFormatter: function (row) {
                        return row.productCode || row.id;
                    },
                    onLoadSuccess : function(data) {
                        if (!Gikam.isEmpty(data)) {
                            this.activeRowByIndex(0);
                        } else {
                            if (_this.param.tabIndex === 0){
                                _this.loadProductList();
                            }
                        }
                    },
                    onRowActive : function(index, row) {
                        if (_this.param.tabIndex === 0){
                            _this.loadProductList(row);
                        }
                    },
                    toolbar : [ {
                        type : 'button',
                        text : 'GIKAM.BUTTON.INSERT',
                        icon : 'add',
                        onClick : function() {
                            Gikam.preInsert({
                                modalTitle : 'GIKAM.BUTTON.INSERT',
                                fields : limsProductMain.getBaseInfoFormFields(),
                                url : limsProductMain.baseUrl,
                                titleWidth : 100,
                                param : {
                                    status : '1'
                                }
                            }).done(function(id) {
                                Gikam.getComp('lims-product-main-edit-list-grid').refresh();
                            });
                        }
                    }, {
                        type : 'button',
                        text : 'GIKAM.BUTTON.DELETE',
                        icon : 'remove-row',
                        onClick : function() {
                            Gikam.getComp('lims-product-main-edit-list-grid').deleteRows(limsProductMain.baseUrl);
                        }
                    }, {
                        type : 'button',
                        text : 'GIKAM.BUTTON.DEACTIVE',
                        icon : 'disable',
                        onClick : function() {
                            let rows = Gikam.getComp('lims-product-main-edit-list-grid').getSelections();
                            if (Gikam.isEmpty(rows)) {
                                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200);
                                return;
                            }
                            var chooseData = rows.map(item => {
                                return {
                                    id : item.id,
                                    status : '0'
                                }
                            })
                            Gikam.put(limsProductMain.baseUrl + '/instant', Gikam.getJsonWrapper({}, [null, chooseData])).done(function (data) {
                                Gikam.getComp('lims-product-main-edit-list-grid').refresh();
                            });
                        }
                    }]
                }]
            },{
                title : 'GIKAM.ACTIVE.STATUS.NO',
                items : [{
                    type : 'grid',
                    id : 'lims-product-main-disable-list-grid',
                    url : limsProductMain.baseUrl + '/queries/raw',
                    requestData : {
                        status_EQ : '0'
                    },
                    service : 'limsProductMainServiceImpl',
                    dbTable : 'T_LIMS_PRODUCT_MAIN',
                    columns : limsProductMain.getEditListColumns(),
                    page : true,
                    onLoadSuccess : function(data) {
                        if (!Gikam.isEmpty(data)) {
                            this.activeRowByIndex(0);
                        } else {
                            if (_this.param.tabIndex === 1){
                                _this.loadProductList();
                            }
                        }
                    },
                    onRowActive : function(index, row) {
                        if (_this.param.tabIndex === 1){
                            _this.loadProductList(row);
                        }
                    },
                    toolbar : [{
                        type : 'button',
                        text : 'GIKAM.BUTTON.ACTIVE',
                        icon : 'enable',
                        onClick : function() {
                            let rows = Gikam.getComp('lims-product-main-disable-list-grid').getSelections();
                            if (Gikam.isEmpty(rows)) {
                                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200);
                                return;
                            }
                            var chooseData = rows.map(item => {
                                return {
                                    id : item.id,
                                    status : '1'
                                }
                            })
                            Gikam.put(limsProductMain.baseUrl + '/instant', Gikam.getJsonWrapper({}, [null, chooseData])).done(function (data) {
                                Gikam.getComp('lims-product-main-disable-list-grid').refresh();
                            });
                        }
                    } ]
                }]
            }]
        }
    },

    loadProductList : function(row) {
        Gikam.getAsyncComp('lims-product-edit-list-grid').done(function(grid) {
            if (row){
                grid.refresh({
                    url : limsProductMain.productUrl + '/queries/raw',
                    requestData: {
                        productMainId_EQ: row.id
                    }
                });
            }else{
                grid.cleanData();
            }
        });
    },

    getProductGridParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'lims-product-edit-list-grid',
            service : 'limsProductServiceImpl',
            dbTable : 'T_LIMS_PRODUCT',
            columns : limsProductMain.getProductListColumns(),
            deleteFormatter: function (row) {
                return row.etCode || row.id;
            },
            onUpdated: function (data) {
                this.refreshRowById(data.id);
            },
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    let row = Gikam.getComp('lims-product-main-edit-list-grid').getActivedRow();
                    if (Gikam.isEmpty(row)) {
                        return;
                    }
                    Gikam.preInsert({
                        modalTitle : 'GIKAM.BUTTON.INSERT',
                        fields : limsProductMain.getProductFormFields(),
                        url : limsProductMain.productUrl,
                        disableNotEmptyValidate: true,
                        titleWidth : 100,
                        param : {
                            productMainId : row.id,
                            productCode : row.productCode
                        }
                    }).done(function(id) {
                        Gikam.getComp('lims-product-edit-list-grid').refresh();
                    });
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    Gikam.getComp('lims-product-edit-list-grid').deleteRows(limsProductMain.productUrl);
                }
            } ]
        }
    },

    create : function() {
        var _this = this;
        Gikam.create('layout', {
            id : 'lims-product-main-edit-list-layout',
            renderTo : workspace.window.$dom,
            west : {
                width : '40%',
                items : [ this.getGridParam() ]
            },
            center : {
                title : 'T_LIMS_PRODUCT.SERVICE_NAME',
                items : [ this.getProductGridParam() ]
            }
        });
    },

    init : function(param) {
        this.param = param;
        this.param.tabIndex = 0;
        this.create();
    }
};
